package com.daxia.ogmall.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.daxia.ogmall.daobase.GenericDAOHibernate;
import com.daxia.ogmall.dto.SpecificationDTO;
import com.daxia.ogmall.model.Specification;
import com.daxia.ogmall.support.Page;

@Repository
public class SpecificationDAO extends GenericDAOHibernate<Specification> {

    public List<Specification> find(SpecificationDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From Specification n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getProduct() != null) {
        	if (dto.getProduct().getId() != null) {
	            hql.append(" and n.product.id = ? ");
	            paras.add(dto.getProduct().getId());
        	}
        }			
        if (StringUtils.isNotBlank(dto.getFactoryCode())) {
            hql.append(" and n.factoryCode = ? ");
            paras.add(dto.getFactoryCode());
        }		
        if (StringUtils.isNotBlank(dto.getOgCode())) {
        	hql.append(" and n.ogCode = ? ");
        	paras.add(dto.getOgCode());
        }
        if (StringUtils.isNotBlank(dto.getSpec1())) {
            hql.append(" and n.spec1 = ? ");
            paras.add(dto.getSpec1());
        }					
        if (StringUtils.isNotBlank(dto.getSpec2())) {
            hql.append(" and n.spec2 = ? ");
            paras.add(dto.getSpec2());
        }					
        
        hql.append(" and n.product.deleted = 0 ");
        
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public List<Specification> getSpecificationByIds(List<Long> ids) {
    	StringBuilder hql = new StringBuilder();
    	hql.append(" from Specification where id in(:ids) ");
    	Query query = getSession().createQuery(hql.toString());
    	query.setParameterList("ids", ids);
    	return query.list();
    }
    
    public Specification findOne(SpecificationDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<Specification> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }
    
    public Specification findSeq(SpecificationDTO dto) {
    	StringBuffer buff = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
		buff.append("SELECT IFNULL(MAX(seq),0) seq from specification s INNER JOIN product p ON p.id = s.product_id where 1=1 ");
		if (dto.getProduct().getBrand() != null) {
			buff.append(" and p.brand_id = ? ");
            paras.add(dto.getProduct().getBrand().getId());
        }
		buff.append(" and p.deleted = 0 ");
		List<Specification> list = super.findBySQL(buff.toString(), paras.toArray(), Specification.class);
		return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }
}
